package com.prs.crm.dao.ca.work.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.DemandInventoryDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.DemandInventory;

public class DemandInventoryDaoImpl extends BaseDaoImpl<DemandInventory>
		implements DemandInventoryDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<DemandInventory> getAllDemandInventory() {
		return (List<DemandInventory>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(DemandInventory.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public DemandInventory getDemandInventoryByName(final String diArticleName) {
		return (DemandInventory) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(DemandInventory.class);
				criteria.add(Restrictions.eq("diArticleName", diArticleName));
				List<DemandInventory> demand = criteria.list();
				if (demand.size() == 0) {
					return null;
				} else {
					return demand.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public DemandInventory getDemandInventoryByStandard(
			final String diArticleStandard) {
		return (DemandInventory) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(DemandInventory.class);
				criteria.add(Restrictions.eq("diArticleStandard",
						diArticleStandard));
				List<DemandInventory> demand = criteria.list();
				if (demand.size() == 0) {
					return null;
				} else {
					return demand.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public DemandInventory getDemandInventoryByTypeId(final int diArticletypeId) {
		return (DemandInventory) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(DemandInventory.class);
				criteria.add(Restrictions
						.eq("diArticletypeId", diArticletypeId));
				List<DemandInventory> demand = criteria.list();
				if (demand.size() == 0) {
					return null;
				} else {
					return demand.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public DemandInventory getDemandInventoryByNames(final String name) {
		Criteria criteria = this.getSession().createCriteria(DemandInventory.class);
		criteria.add(Restrictions.eq("di_articleName", name));
		List<DemandInventory> demand = criteria.list();
		if (demand==null || demand.size()==0) {
			return null;
		}
		else{
			return demand.get(0);
		}
	}
}
